Passed
Push — develop ( 9a1b4b...96b452 )
by Xaver
03:40
created

helper.js ➔ showStat   A

Complexity

Conditions 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
rs 9.85
c 0
b 0
f 0
cc 2
1
'use strict';
2
3
define({
4
  get: function get(url) {
5
    return new Promise(function (resolve, reject) {
6
      var req = new XMLHttpRequest();
7
      req.open('GET', url);
8
9
      req.onload = function onload() {
10
        if (req.status === 200) {
11
          resolve(req.response);
12
        } else {
13
          reject(Error(req.statusText));
14
        }
15
      };
16
17
      req.onerror = function onerror() {
18
        reject(Error('Network Error'));
19
      };
20
21
      req.send();
22
    });
23
  },
24
25
  getJSON: function getJSON(url) {
26
    return require('helper').get(url).then(JSON.parse);
27
  },
28
29
  sortByKey: function sortByKey(key, d) {
30
    return d.sort(function (a, b) {
31
      return b[key] - a[key];
32
    });
33
  },
34
35
  limit: function limit(key, m, d) {
36
    return d.filter(function (n) {
37
      return n[key].isAfter(m);
38
    });
39
  },
40
41
  sum: function sum(a) {
42
    return a.reduce(function (b, c) {
43
      return b + c;
44
    }, 0);
45
  },
46
47
  one: function one() {
48
    return 1;
49
  },
50
51
  dictGet: function dictGet(dict, key) {
52
    var k = key.shift();
53
54
    if (!(k in dict)) {
55
      return null;
56
    }
57
58
    if (key.length === 0) {
59
      return dict[k];
60
    }
61
62
    return this.dictGet(dict[k], key);
63
  },
64
65
  listReplace: function listReplace(s, subst) {
66
    for (var key in subst) {
67
      if (subst.hasOwnProperty(key)) {
68
        var re = new RegExp(key, 'g');
69
        s = s.replace(re, subst[key]);
70
      }
71
    }
72
    return s;
73
  },
74
75
  hasLocation: function hasLocation(d) {
76
    return 'location' in d &&
77
      Math.abs(d.location.latitude) < 90 &&
78
      Math.abs(d.location.longitude) < 180;
79
  },
80
81
  subtract: function subtract(a, b) {
82
    var ids = {};
83
84
    b.forEach(function (d) {
85
      ids[d.node_id] = true;
86
    });
87
88
    return a.filter(function (d) {
89
      return !ids[d.node_id];
90
    });
91
  },
92
93
  /* Helpers working with links */
94
95
  showDistance: function showDistance(d) {
96
    if (isNaN(d.distance)) {
97
      return '';
98
    }
99
100
    return d.distance.toFixed(0) + ' m';
101
  },
102
103
  showTq: function showTq(d) {
104
    return (d * 100).toFixed(0) + '%';
105
  },
106
107
  attributeEntry: function attributeEntry(V, children, label, value) {
108
    if (value !== undefined) {
109
      if (typeof value !== 'object') {
110
        value = V.h('td', value);
111
      }
112
113
      children.push(V.h('tr', [
114
        V.h('th', _.t(label)),
115
        value
116
      ]));
117
    }
118
  },
119
  showStat: function showStat(V, o, subst) {
120
    var content = V.h('img', { attrs: { src: require('helper').listReplace(o.image, subst), width: o.width, height: o.height, alt: _.t('loading', { name: o.name }) } });
121
122
    if (o.href) {
123
      return V.h('div', V.h('a', {
124
        attrs:
125
          {
126
            href: require('helper').listReplace(o.href, subst),
127
            target: '_blank',
128
            title: require('helper').listReplace(o.title, subst)
129
          }
130
      }, content));
131
    }
132
    return V.h('div', content);
133
  },
134
135
  getTileBBox: function getTileBBox(s, map, tileSize, margin) {
136
    var tl = map.unproject([s.x - margin, s.y - margin]);
137
    var br = map.unproject([s.x + margin + tileSize, s.y + margin + tileSize]);
138
139
    return { minX: br.lat, minY: tl.lng, maxX: tl.lat, maxY: br.lng };
140
  },
141
  positionClients: function positionClients(ctx, p, startAngle, node, startDistance) {
142
    if (node.clients === 0) {
143
      return;
144
    }
145
146
    var radius = 3;
147
    var a = 1.2;
148
    var mode = 0;
149
150
    ctx.beginPath();
151
    ctx.fillStyle = config.client.wifi24;
152
153
    for (var orbit = 0, i = 0; i < node.clients; orbit++) {
154
      var distance = startDistance + orbit * 2 * radius * a;
155
      var n = Math.floor((Math.PI * distance) / (a * radius));
156
      var delta = node.clients - i;
157
158
      for (var j = 0; j < Math.min(delta, n); i++, j++) {
159
        if (mode !== 1 && i >= (node.clients_wifi24 + node.clients_wifi5)) {
160
          mode = 1;
161
          ctx.fill();
162
          ctx.beginPath();
163
          ctx.fillStyle = config.client.wifi5;
164
        } else if (mode === 0 && i >= node.clients_wifi24) {
165
          mode = 2;
166
          ctx.fill();
167
          ctx.beginPath();
168
          ctx.fillStyle = config.client.other;
169
        }
170
        var angle = 2 * Math.PI / n * j;
171
        var x = p.x + distance * Math.cos(angle + startAngle);
172
        var y = p.y + distance * Math.sin(angle + startAngle);
173
174
        ctx.moveTo(x, y);
175
        ctx.arc(x, y, radius, 0, 2 * Math.PI);
176
      }
177
    }
178
    ctx.fill();
179
  },
180
  fullscreen: function fullscreen(btn) {
181
    if (!document.fullscreenElement && !document.webkitFullscreenElement && !document.mozFullScreenElement) {
182
      var fel = document.firstElementChild;
183
      var func = fel.requestFullscreen
184
        || fel.webkitRequestFullScreen
185
        || fel.mozRequestFullScreen;
186
      func.call(fel);
187
      btn.classList.remove('ion-full-enter');
188
      btn.classList.add('ion-full-exit');
189
    } else {
190
      func = document.exitFullscreen
191
        || document.webkitExitFullscreen
192
        || document.mozCancelFullScreen;
193
      if (func) {
194
        func.call(document);
195
        btn.classList.remove('ion-full-exit');
196
        btn.classList.add('ion-full-enter');
197
      }
198
    }
199
  },
200
  escape: function escape(string) {
201
    return string.replace(/</g, '&lt;')
202
      .replace(/>/g, '&gt;')
203
      .replace(/"/g, '&#34;')
204
      .replace(/'/g, '&#39;');
205
  }
206
});
207